//============================================================================
// Name        : Game.cpp
// Author      : Piotr Brygidyn
// Version     : 1.0
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "Game.h"
#include "Player.h"
#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

Game::Game() {
        srand(time(NULL));
}

Game::~Game() {
        if(!players.empty())
        {
                for (vector<Player *>::iterator it = players.begin(); it != players.end(); ++it)
                {
                        delete (*it);
                }
        }
}

void Game::addPlayer(string name) {
        cout << "Adding player: " << name << endl;
        Player * newPlayer = new Player(name);
        players.push_back(newPlayer);
}

void Game::play() {
        //each player throws a pair of dices
        for (vector<Player *>::iterator it = players.begin(); it != players.end(); ++it)
        {
                int result = (*it)->draw();
                if(result == 7 || result == 11)
                {
                        (*it)->setWinner() = true;
                        setResultsForOthers(it, false);
                }
                else if(result == 2)
                {
                        (*it)->setWinner() = false;
                        setResultsForOthers(it, true);
                }
                else
                {
                        (*it)->setWinner() = false;
                }
        }
}

void Game::setResultsForOthers(vector<Player*>::iterator thisOne, bool state) {
        for (vector<Player *>::iterator it = players.begin(); it != players.end(); ++it)
        {
                if(it != thisOne)
                {
                        (*it)->setWinner() = state;
                }
        }
}

void Game::printResults(void) {
        cout << "=========RESULTS===========" << endl;
        for (vector<Player *>::iterator it = players.begin(); it != players.end(); ++it)
        {
                if((*it)->getWinner() == true)
                {
                        cout << "Player: " << (*it)->getName() << " won the game ! " << endl;
                }
                else
                {
                        cout << "Player: " << (*it)->getName() << " didnt win the game ! " << endl;
                }
        }
        cout << "==========================" << endl;
}


